//
//  ViewController.m
//  UI22_AFNetWorking
//
//  Created by dllo on 16/8/12.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "HotCar.h"


@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    [self netWorkPost];
    [self netWorkGET];
//    [self createTableView];
   }

- (void)createTableView {
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.textLabel.text = [self.dataArray[indexPath.row] name];
   
    return cell;
}

// AFNetWorking POST
- (void)netWorkPost {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *postStr = @"http://open3.bantangapp.com/base/app/init";
    NSString *parmeters = @"app_id=com.jzyd.BanTang&app_installtime=1467276561&app_versions=5.8.5&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&device_token=1aa2741a8e58dbf700d96e588b551a5bf7f8a1e222eb9948c836ba717ce0d48e&os_versions=10.0&screensize=750&track_device_info=iPhone8%2C1&track_deviceid=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&v=17";
    // 设置请求参数的字符串格式 自定义
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return parmeters;
    }];
    // 发送post请求
    [manager POST:postStr parameters:parmeters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"请求成功%@", task.currentRequest);
        NSLog(@"请求成功: %@", responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败; %@", error);
    }];

}

// AFNetWorking GET
- (void)netWorkGET {
    self.dataArray = [NSMutableArray array];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 添加请求头 设置requestSerializer中的一个字典
    NSString *URLStr = @"http://api.dongting.com/frontpage/frontpage?location=0&version=1470998222737&app=ttpod&v=v9.0.4.2016070616&user_id=0&mid=iPhone8%2C1&f=f320&s=s310&imsi=&hid=&splus=10.0&active=1&net=2&openudid=4bbd8925c4a3991ee51c9cd8b9a8dc5961820764&idfa=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&utdid=V62kivWPJBYDABK%2FL8gCUnDI&alf=(null)&bundle_id=com.ttpod.music&latitude=(null)&longtitude=(null)&access_token=(null)&vc=9000400";
//    @"http://223.99.255.20/cars.app.autohome.com.cn/dealer_v6.0.0/dealer/hotbrands-pm2.json";
    // requestSerializer 格式化 body/参数
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    // 设置请求头 就相当于设置一个标识(一个网址可以做很多事情) 对应不同的标识 来让网址可以有不同的实现
    // [manager.requestSerializer setValue:@"lalala" forHTTPHeaderField:@"name"];
    // 设置 超时时间
    manager.requestSerializer.timeoutInterval = 90;
    // 2. responseSerializer 负责格式化返回的数据(设置将返回的数据的格式)
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    // responseSerializer 默认类型 AFJSONRequestSerializer
    // 保留系统的contentType添加新的type
    NSMutableSet *set = [NSMutableSet setWithSet:manager.responseSerializer.acceptableContentTypes];
    [set addObject:@"text/html"];
    [manager.responseSerializer setAcceptableContentTypes:set];
    
  [manager GET:URLStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", responseObject);

//        NSLog(@"%@", task.currentRequest.URL);
//        NSLog(@"%@", URLStr);
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
//        NSArray *array = responseObject[@"result"][@"list"];
//        for (NSDictionary *dic in array) {
//            HotCar *hotCar = [HotCar hotCarWithDic:dic];
//            [self.dataArray addObject:hotCar];
//        }
//        dispatch_async(dispatch_get_main_queue(), ^{
//            [self.tableView reloadData];
//        });
        
//                NSLog(@"请求成功: %@", responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"%@", URLStr);
//        NSLog(@"%@", task.currentRequest.URL);
//        NSLog(@"请求失败: %@", error);
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
